{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import iplantuml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 数学模型(百鳮百钱)\n",
    "\n",
    "$\n",
    "\\begin{cases}\n",
    "    cock + hen + chichen = 100 \\\\\n",
    "    5 \\times cock + 3 \\times hen + \\cfrac{chiken}{3} = 100\n",
    "\\end{cases}\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 算法设计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing output for /home/jack/Desktop/structures-algorithms/21dabfcd-f820-4c79-a50d-6ce42e281127.uml to 21dabfcd-f820-4c79-a50d-6ce42e281127.svg\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"398px\" preserveAspectRatio=\"none\" style=\"width:273px;height:398px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 273 398\" width=\"273px\" zoomAndPan=\"magnify\"><defs/><g><ellipse cx=\"148.0303\" cy=\"20\" fill=\"#222222\" rx=\"10\" ry=\"10\" style=\"stroke:#222222;stroke-width:1.0;\"/><rect fill=\"#F1F1F1\" height=\"33.9688\" rx=\"12.5\" ry=\"12.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"174.0605\" x=\"61\" y=\"166.5151\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"154.0605\" x=\"71\" y=\"187.6538\">chiken = 100 - cock - hen</text><rect fill=\"#F1F1F1\" height=\"33.9688\" rx=\"12.5\" ry=\"12.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"68.0004\" x=\"114.0301\" y=\"264.4839\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"48.0004\" x=\"124.0301\" y=\"285.6226\">生成组合</text><polygon fill=\"#F1F1F1\" points=\"126.0304,220.4839,170.0301,220.4839,182.0301,232.4839,170.0301,244.4839,126.0304,244.4839,114.0304,232.4839,126.0304,220.4839\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"43.9997\" x=\"126.0304\" y=\"236.292\">满足条件</text><polygon fill=\"#F1F1F1\" points=\"148.0303,318.4526,160.0303,330.4526,148.0303,342.4526,136.0303,330.4526,148.0303,318.4526\" style=\"stroke:#181818;stroke-width:0.5;\"/><polygon fill=\"#F1F1F1\" points=\"101.8818,110.8047,194.1787,110.8047,206.1787,122.8047,194.1787,134.8047,101.8818,134.8047,89.8818,122.8047,101.8818,110.8047\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"24.981\" x=\"152.0303\" y=\"145.0151\">True</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"92.2969\" x=\"101.8818\" y=\"126.6128\">hen in range(34)</text><polygon fill=\"#F1F1F1\" points=\"99.6394,50,196.4211,50,208.4211,62,196.4211,74,99.6394,74,87.6394,62,99.6394,50\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"24.981\" x=\"152.0303\" y=\"84.2104\">True</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"96.7817\" x=\"99.6394\" y=\"65.8081\">cock in range(21)</text><ellipse cx=\"24\" cy=\"109\" fill=\"none\" rx=\"11\" ry=\"11\" style=\"stroke:#222222;stroke-width:1.0;\"/><ellipse cx=\"24\" cy=\"109\" fill=\"#222222\" rx=\"6\" ry=\"6\" style=\"stroke:#222222;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"244.4839\" y2=\"264.4839\"/><polygon fill=\"#181818\" points=\"144.0303,254.4839,148.0303,264.4839,152.0303,254.4839,148.0303,258.4839\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"182.0301\" x2=\"194.0301\" y1=\"232.4839\" y2=\"232.4839\"/><polygon fill=\"#181818\" points=\"190.0301,271.4683,194.0301,281.4683,198.0301,271.4683,194.0301,275.4683\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"194.0301\" x2=\"194.0301\" y1=\"232.4839\" y2=\"330.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"194.0301\" x2=\"160.0303\" y1=\"330.4526\" y2=\"330.4526\"/><polygon fill=\"#181818\" points=\"170.0303,326.4526,160.0303,330.4526,170.0303,334.4526,166.0303,330.4526\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"298.4526\" y2=\"318.4526\"/><polygon fill=\"#181818\" points=\"144.0303,308.4526,148.0303,318.4526,152.0303,308.4526,148.0303,312.4526\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"200.4839\" y2=\"220.4839\"/><polygon fill=\"#181818\" points=\"144.0303,210.4839,148.0303,220.4839,152.0303,210.4839,148.0303,214.4839\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"134.8047\" y2=\"166.5151\"/><polygon fill=\"#181818\" points=\"144.0303,156.5151,148.0303,166.5151,152.0303,156.5151,148.0303,160.5151\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"342.4526\" y2=\"352.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"247.0605\" y1=\"352.4526\" y2=\"352.4526\"/><polygon fill=\"#181818\" points=\"243.0605,242.5815,247.0605,232.5815,251.0605,242.5815,247.0605,238.5815\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"247.0605\" x2=\"247.0605\" y1=\"122.8047\" y2=\"352.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"247.0605\" x2=\"206.1787\" y1=\"122.8047\" y2=\"122.8047\"/><polygon fill=\"#181818\" points=\"216.1787,118.8047,206.1787,122.8047,216.1787,126.8047,212.1787,122.8047\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"89.8818\" x2=\"49\" y1=\"122.8047\" y2=\"122.8047\"/><polygon fill=\"#181818\" points=\"45,228.5815,49,238.5815,53,228.5815,49,232.5815\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"49\" x2=\"49\" y1=\"122.8047\" y2=\"364.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"49\" x2=\"259.0605\" y1=\"364.4526\" y2=\"364.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"259.0605\" x2=\"259.0605\" y1=\"62\" y2=\"364.4526\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"259.0605\" x2=\"208.4211\" y1=\"62\" y2=\"62\"/><polygon fill=\"#181818\" points=\"218.4211,58,208.4211,62,218.4211,66,214.4211,62\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"74\" y2=\"110.8047\"/><polygon fill=\"#181818\" points=\"144.0303,100.8047,148.0303,110.8047,152.0303,100.8047,148.0303,104.8047\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"87.6394\" x2=\"24\" y1=\"62\" y2=\"62\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"24\" x2=\"24\" y1=\"62\" y2=\"98\"/><polygon fill=\"#181818\" points=\"20,88,24,98,28,88,24,92\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"148.0303\" x2=\"148.0303\" y1=\"30\" y2=\"50\"/><polygon fill=\"#181818\" points=\"144.0303,40,148.0303,50,152.0303,40,148.0303,44\" style=\"stroke:#181818;stroke-width:1.0;\"/><!--SRC=[Aov9B2fHu2hFoCn9rKZEJyvMoCnJA4hCIq_LC3BKrCmirWWfAatLv5800h0Y1OsCr3o48cCJD4KWO9MSaPaDL6MhO6XWeA2h03PNLm6eqHgk9ZDDuzdkXI-sRNusT-6JtTiGci46F9yo_rd7XEUxMvvEw43eISrBoKpZWhB0hk62CufByWi0]--></g></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%plantuml\n",
    "@startuml\n",
    "start \n",
    "while(cock in range(21))is(True)\n",
    "    while (hen in range(34))is(True)\n",
    "        :chiken = 100 - cock - hen;\n",
    "        if(满足条件)\n",
    "            :生成组合;\n",
    "        endif\n",
    "    endwhile\n",
    "endwhile\n",
    "stop\n",
    "@enduml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 25, 75)\n",
      "(4, 18, 78)\n",
      "(8, 11, 81)\n",
      "(12, 4, 84)\n"
     ]
    }
   ],
   "source": [
    "def func():\n",
    "    for cock in range(21):\n",
    "        for hen in range(34):\n",
    "            for chiken in range(101):\n",
    "                if all([\n",
    "                    cock + hen + chiken == 100,\n",
    "                    chiken % 3 == 0,\n",
    "                    5 * cock + 3 * hen + chiken/3 == 100,\n",
    "                ]):\n",
    "                    yield (cock, hen, chiken)\n",
    "\n",
    "for item in func():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 25, 75)\n",
      "(4, 18, 78)\n",
      "(8, 11, 81)\n",
      "(12, 4, 84)\n"
     ]
    }
   ],
   "source": [
    "def func():\n",
    "    for cock in range(21):\n",
    "        for hen in range(34):\n",
    "            chiken = 100 - cock - hen \n",
    "            if all([\n",
    "                cock + hen + chiken == 100,\n",
    "                chiken % 3 == 0,\n",
    "                5*cock + 3*hen + chiken/3==100,\n",
    "            ]):\n",
    "                yield(cock, hen, chiken)\n",
    "\n",
    "for item in func():\n",
    "    print(item)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
